package popcorn.ticket.controller;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.entity.Logs;
import popcorn.ticket.entity.MovieComment;
import popcorn.ticket.entity.Users;
import popcorn.ticket.service.MovieCommentService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

/**
 * 影评表控制器类
 * 
 * @author 陈雄伟
 *
 */
@Controller
public class MovieCommentController {

	@Autowired
	private MovieCommentService mcs;

	/**
	 * 根据关键字模糊查询分页
	 * @param keywords
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "getMC", produces = "application/json;charset = utf-8")
	@ResponseBody
	public PageData<MovieComment> getMovieComment(@RequestParam(value = "keywords", defaultValue = "") String keywords,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit) {
		
		PageData<MovieComment> pd = mcs.getCommentByAll(keywords, page, limit);

		return pd;
	}

	/**
	 * 修改状态
	 * @param idlist
	 * @return
	 */
	@RequestMapping(value = "updateMcSta", produces = "application/json;charset = utf-8", consumes = "application/json")
	@ResponseBody
	public AjaxResponse uodateMcStatus(@RequestBody String idlist) {
		// 实例化一个ArrayList对象，用来要批量删除的集合
		List list = new ArrayList<>();
		if (idlist.matches("[0-9]+")) {
			// 如果是数字
			list.add(idlist);
		} else {
			// 将前台的传送过来的数据进行剪切
			idlist = idlist.substring(0, idlist.length() - 2);
			idlist = idlist.substring(1, idlist.length());
			String[] arr = idlist.split(",");
			// 将剪切的的id存放进集合中
			for (int i = 0; i < arr.length; i++) {
				
				list.add(arr[i]);

			}
		}
		// 实例化一个Logs对象,存放，并赋值
		Logs logs = new Logs();
		// 在这里面进行异常处理，出现异常就表示出错，前台事务回滚
		try {
			// 给log赋值
			logs.setLogip(InetAddress.getLocalHost() + "");
			logs.setLogobject("moviecomment");
			logs.setLogop("禁用评论");
			logs.setRemark("评论违规");
			logs.setLogtime(new Date());
			Users u = new Users();
			// 这个要从前台获取信息在加入
			u.setUserid(1);
			logs.setUsers(u);
			return mcs.updateStatus(list, logs);
		} catch (Exception e) {
			AjaxResponse ar = new AjaxResponse();
			ar.setMsg("禁用失败");
			ar.setCode(0);
			return ar;
		}
	}
	/**
	 * 查单个用户的评论记录
	 * @param userid
	 * @param page
	 * @return
	 */
	@RequestMapping("getCommentByUid/{userid}")
	@ResponseBody
	public List<MovieComment> getCommentByUserid(@PathVariable("userid") int userid,
			@RequestParam(defaultValue = "1")int page){
		PageHelper.startPage(page, 3);
		List<MovieComment> list = mcs.selCommentByUserid(userid);
		return list;
	}
	/**
	 * 查单个用户的总评论记录
	 * @param userid
	 * @return
	 */
	@RequestMapping("countCommentByUid/{userid}")
	@ResponseBody
	public int countCommentByUid(@PathVariable("userid") int userid) {
		List<MovieComment> list = mcs.selCommentByUserid(userid);
		PageInfo<MovieComment> pi = new PageInfo<MovieComment>(list);
		return (int)pi.getTotal();
	}
	
	@RequestMapping("delCommentByUid")
	@ResponseBody
	public AjaxResponse delComment(@RequestBody MovieComment comment) {
		Logs logs = new Logs();
		try {
			// 给log赋值
			logs.setLogip(InetAddress.getLocalHost() + "");
			logs.setLogobject("moviecomment");
			logs.setLogop("禁用评论");
			logs.setRemark("用户删评");
			logs.setLogtime(new Date());
			Users u = new Users();
			// 这个要从前台获取信息在加入
			u.setUserid(comment.getUsers().getUserid());
			logs.setUsers(u);
			boolean flag = mcs.updateStatusByUser(comment, logs);
			if(flag) {
				AjaxResponse ar = new AjaxResponse("删除成功", 200);
				return ar;
			}else {
				AjaxResponse ar = new AjaxResponse("删除失败",0);
				return ar;
			}
			
		} catch (Exception e) {
			AjaxResponse ar = new AjaxResponse("删除失败",0);
			return ar;
		}
	}
	
	@RequestMapping("addComment")
	@ResponseBody
	public AjaxResponse addCommentByUser(@RequestBody MovieComment mc) {
		mc.setCommenttime(new Date());
		mc.setCommentstatus(1);
		return mcs.addCommentByUser(mc);
	}
	
	/**
	 * 根据电影id获取电影的评价
	 * 
	 * @param movieid
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "getMCByMId/{movieid}", produces = "application/json;charset = utf-8")
	@ResponseBody
	public PageData<MovieComment> getMCByMovieId(@PathVariable(value = "movieid") int movieid,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "6") int limit) {
		PageData<MovieComment> pd = mcs.getCommnetByMI(movieid, page, limit);

		return pd;
	}

	/**
	 * 高分排行
	 * 
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "glbs", produces = "application/json")
	@ResponseBody
	public PageData<MovieComment> getListByScore(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "20") int limit) {
		PageData<MovieComment> pd = mcs.getListByScore(page, limit);
		return pd;
	}
	
	/**
	 * 时间区间+（用户名和电影名的模糊查询）+ 用户编号查询
	 * 
	 * @param page      页码
	 * @param limit     每页的个数
	 * @param keywords  关键字
	 * @param startTime 开始时间
	 * @param endTime   结束时间
	 * @param select    选择的选项
	 * @return
	 */
	@RequestMapping(value = "gmcbts", produces = "application/json;charset = utf-8")
	@ResponseBody
	public PageData<MovieComment> getlistData(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "keywords", defaultValue = "") String keywords,
			@RequestParam(value = "startTime", defaultValue = "") String startTime,
			@RequestParam(value = "endTime", defaultValue = "") String endTime,
			@RequestParam(value = "select", defaultValue = "") String select) {

		PageData<MovieComment> pd = null;
		if (select.equals("username")) {
			String username = "1";
			pd = mcs.getCommnetsByTimeSlot(startTime, endTime, "", 0, username, page, limit, keywords);
		} else if (select.equals("moviename")) {
			String moviename = "1";
			pd = mcs.getCommnetsByTimeSlot(startTime, endTime, moviename, 0, "", page, limit, keywords);
		} else if (select.equals("userid")) {
			int userid = 1;
			pd = mcs.getCommnetsByTimeSlot(startTime, endTime, "", userid, "", page, limit, keywords);
		} else if (select.equals("")) {
			pd = mcs.getCommnetsByTimeSlot(startTime, endTime, "1", 0, "", page, limit, keywords);
		}

		return pd;
	}

}
